const app = getApp()

Page({
	data: {
		isIphoneX: app.globalData.isIphoneX, 
		listData: [], 
		extraNodes: [ 
			{
				type: "after",
				dragId: "plus",
				slot: "plus",
				fixed: true
			}
		],
		pageMetaScrollTop: 0,
		scrollTop: 0
	},
	sortEnd(e) { 
		this.setData({
			listData: e.detail.listData
		});
	}, 
	change(e) {
    this.setData({
      listData: e.detail.listData
    });
    this.drag.init()
	},
	sizeChange(e) {
		wx.pageScrollTo({scrollTop: 0})
		this.setData({
			size: e.detail.value
		});
		this.drag.init(); 
	},
 
	itemClick(e) {
		console.log(e);
	},
	toggleFixed(e) {
		let key = e.currentTarget.dataset.key;
		let {listData} = this.data;
		listData[key].fixed = !listData[key].fixed
		this.setData({
			listData: listData
		});
		this.drag.init();
	},
	add(e) {
		let listData = this.data.listData;
    var that=this 
    wx.chooseImage({
      count:20,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        console.log(res, 'resresres')   
        var tempFilePaths = res.tempFilePaths; 
        console.log(tempFilePaths,'tempFilePaths')
        that.uploadFile(res.tempFilePaths)
        // for (var i = 0; i < tempFilePaths.length; i++) {  
        //  var item={
        //       dragId: "item"+i, 
        //      imgPath: tempFilePaths[i],
        //       fixed: false
        //   }  
        //    listData.push(item)
        // } 
        // that.setData({ listData }); 
    
      }
    });
 
	},


  //上传图片
  uploadFile: function (pics) {
    var that = this;
    var listData = this.data.listData
    for (var i = 0; i < pics.length; i++) { 
      // (function (i) {
      //   setTimeout(function () { 
          wx.uploadFile({
            url: app.globalData.apiUrl + '/gds/protected/comm/file/gds.GoodsImgVO/fileUpload',
            header: {
              "Authorization": wx.getStorageSync('Authorization'),
            },
            formData: {
              width: 750
            },
            filePath: pics[i],
            name: 'file',
            success: function (res) {
              if (typeof res.data == "string") {
                var resdata = JSON.parse(res.data)
              }
              if (resdata.success) {
                var item = {
                  imgName: resdata.datas.vo.fileName,
                  imgPath: resdata.datas.vo.relativeFilePath,
                  fixed: false
                }
                listData.push(item) 
                that.setData({
                  listData
                })
                that.drag.init(); 
              }
            },
            fail: function (res) {
              // wx.showToast({
              //   icon: 'none',
              //   title: '图片上传失败',
              // })
            }
          })
      //   }, (i + 1) * 800);
      // })(i)
    }
  },



	scroll(e) {
		this.setData({
			pageMetaScrollTop: e.detail.scrollTop
		})
	},
	// 页面滚动
	onPageScroll(e) {
		this.setData({
			scrollTop: e.scrollTop
		});
	},
	onLoad() {
		this.drag = this.selectComponent('#drag');
    this.drag.init();
	}
})
